#!/bin/bash
# $Id: bopt_apply.sh 5 2008-10-28 18:06:25Z shawnchin $
# ===========================================================================
# Copyright (c) 2008, Lee-Shawn Chin
# All rights reserved.

# Redistribution and use in source and binary forms, with or without 
# modification, are permitted provided that the following conditions 
# are met:
#
#  * Redistributions of source code must retain the above copyright 
#    notice, this list of conditions and the following disclaimer.
#  * Redistributions in binary form must reproduce the above copyright 
#    notice, this list of conditions and the following disclaimer in 
#    the documentation and/or other materials provided with the distribution.
#  * The names of the its contributors may be used to endorse or promote 
#    products derived from this software without specific prior written 
#    permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
# ===========================================================================

# if default options required and not yet added
if $_bopt_defopts && ! $_bopt_defopts_applied; then
    bopt_add_option "help"    "h" 0 "Show this help page and exit"
    bopt_add_option "version" "V" 0 "Show version information and exit" 
    _bopt_defopts_applied=true
fi

# translate _opt_*[] to o_OPTNAME and v_OPTNAME
_opt_index=0
while [ $_opt_index -lt $_opt_size ]; do
    # define user accessible vars
    eval "export o_${_opt_long[$_opt_index]}=${_opt_selected[$_opt_index]}"
    eval "export v_${_opt_long[$_opt_index]}=\"${_opt_argval[$_opt_index]}\""
    
    # define internal mapping from sopt/lopt to array index
    eval "_map_stoi_${_opt_short[$_opt_index]}=$_opt_index"
    eval "_map_stol_${_opt_short[$_opt_index]}=${_opt_long[$_opt_index]}"
    eval "_map_ltoi_${_opt_long[$_opt_index]}=$_opt_index"

    # increment counter
    let _opt_index+=1
done

# set flag
_bopt_parsed=true

# ============= SCRIPT ENDS =================================================

